package com.yhn.init.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yhn.init.common.BaseResponse;
import com.yhn.init.common.DeleteRequest;
import com.yhn.init.common.ResultUtils;
import com.yhn.init.model.dto.orders.*;
import com.yhn.init.model.vo.OrdersVO;
import com.yhn.init.service.DesignService;
import com.yhn.init.service.OrdersService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;


/**
 * 订单表
 *
 * @author yhnyhn123
 * @email 2236751002@qq.com
 * @date 2024-03-08 20:36:42
 */
@RestController
@RequestMapping("orders")
public class OrdersController {

    @Resource
    private OrdersService ordersService;
    @Resource
    private DesignService designService;

    /**
     * 列表,分页
     */
    @GetMapping("/list")
    //@SaCheckPermissions("furniture/orders/list")
    public BaseResponse<Page<OrdersVO>> listByPage(@RequestBody OrdersQueryRequest ordersQueryRequest) {
        Page<OrdersVO> page = ordersService.queryPage(ordersQueryRequest);
        return ResultUtils.success(page);
    }


    /**
     * 获取单个信息
     */
    @GetMapping("/info/{id}")
    //@SaCheckPermissions("furniture/orders/info")
    public BaseResponse<OrdersVO> getInfoById(@PathVariable("id") Long id) {
        OrdersVO orders = ordersService.getInfoById(id);

        return ResultUtils.success(orders);
    }


    /**
     * 保存
     */
    @PostMapping("/save")
    @SaCheckPermission(value = {"/furniture/order/add", "root"}, mode = SaMode.OR)
    public BaseResponse<Long> save(@RequestBody @Valid OrdersAddRequest ordersAddRequest) {
        return ResultUtils.success(ordersService.saveOrders(ordersAddRequest));
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    //@SaCheckPermission("furniture/orders/update")
    public BaseResponse<Boolean> update(@RequestBody OrdersUpdateRequest ordersUpdateRequest) {
        boolean result = ordersService.updateOrders(ordersUpdateRequest);

        return ResultUtils.success(result);
    }


    /**
     * 删除
     */
    @PostMapping("/delete")
    //@SaCheckPermissions("furniture/orders/delete")
    public BaseResponse<Boolean> delete(@RequestBody DeleteRequest deleteRequest) {
        boolean result = ordersService.deleteById(deleteRequest);
        return ResultUtils.success(result);
    }

    /**
     * 批量删除
     */
    @PostMapping("/deleteBatch")
    //@SaCheckPermissions("furniture/orders/delete")
    public BaseResponse<Boolean> deleteBatch(@RequestBody Long[] ids) {
        boolean result = ordersService.deleteBatchByIds(ids);
        return ResultUtils.success(result);
    }

    @PostMapping("/design")
    @SaCheckPermission(value = {"/furniture/orders/design", "root"}, mode = SaMode.OR)
    public BaseResponse<Long> submitDesign(@RequestBody @Valid OrdersDesignRequest designRequest) {
        return ResultUtils.success(designService.submitDesign(designRequest));
    }

    @PostMapping("/confirm")
    @SaCheckPermission(value = {"/furniture/orders/confirm", "root"}, mode = SaMode.OR)
    public BaseResponse<Boolean> confirmOrders(@RequestBody @Valid OrdersConfirmedRequest confirmRequest) {
        return ResultUtils.success(ordersService.confirmOrders(confirmRequest));
    }
}
